﻿using QuanLyKhachSan.Validation;
using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyKhachSan
{
    public class PhanQuyenManager:IValidator<PhanQuyen>,ILenhThucThi<PhanQuyen>
    {

        public ActionResult Them(PhanQuyen obj)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = from q in ql.PhanQuyens
                            where q.MaQuyen.Equals(obj.MaQuyen) && q.MaNhom.Equals(obj.MaNhom)
                            select q;

                try
                {
                    if (!query.Any())
                    {
                        ql.PhanQuyens.Add(obj);
                        ql.SaveChanges();
                    }
                    else
                    {
                        action.Exceptions.Add(new Exception("Đã tồn tại phân quyền có mã nhóm " + obj.MaNhom+" mã quyền "+obj.MaQuyen));
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResult Xoa(string ma)
        {
            throw new NotImplementedException();
        }

        public ActionResult Xoa(PhanQuyen ma)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = (from q in ql.PhanQuyens
                             where q.MaQuyen.Equals(ma.MaQuyen) && q.MaNhom.Equals(ma.MaNhom)
                             select q).FirstOrDefault();
                try
                {
                    if (query == null)
                    {
                        action.Exceptions.Add(new Exception("Không tồn tại phân quyền có mã nhóm " + ma.MaNhom+" mã quyền "+ma.MaQuyen));
                    }
                    else
                    {
                        ql.PhanQuyens.Remove(query);
                        ql.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResult CapNhat(PhanQuyen obj)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = (from q in ql.PhanQuyens
                             where q.MaQuyen.Equals(obj.MaQuyen)
                             select q).FirstOrDefault();
                try
                {
                    if (query == null)
                        action.Exceptions.Add(new Exception("Không tồn tại quyền có mã nhóm " + obj.MaQuyen+" mã quyền "+obj.MaQuyen));
                    else
                    {
                        query.MaNhom = obj.MaNhom;
                        query.MaQuyen = obj.MaQuyen;
                        query.Mota = obj.Mota;
                        ql.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResultAsList<PhanQuyen> LayDanhSach()
        {
            var action = new ActionResultAsList<PhanQuyen>();

            var ql = new QLKhachSanContext();
            try
            {
                var query = from q in ql.PhanQuyens
                            select q;
                action.Result = query.ToList();
                if (action.Result.Count == 0)
                    action.Exceptions.Add(new Exception("Dữ liệu quyền trống"));
            }
            catch (Exception e)
            {
                action.Exceptions.Add(e);
            }

            return action;
        }

        public ValidateResult Validate(PhanQuyen obj)
        {
            throw new NotImplementedException();
        }
    }
}
